package cn.acheng1314.springboot2

import io.swagger.annotations.ApiOperation
import org.mybatis.spring.annotation.MapperScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2


@SpringBootApplication
@EnableWebMvc
@EnableSwagger2
@MapperScan(value = ["cn.acheng1314.springboot2.mapper"])
open class Springboot2Application : WebMvcConfigurer {
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/")
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/")

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/")

        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")
    }


    @Bean(name = ["defaultApi"])
    open fun createRestApi(): Docket {
//        val responseMessageList: MutableList<ResponseMessage> = ArrayList()
//        responseMessageList.add(ResponseMessageBuilder()
//                .code(HttpStatus.UNAUTHORIZED.value())
//                .message(HttpStatus.UNAUTHORIZED.reasonPhrase)
//                .responseModel(ModelRef("ResponseWrap"))
//                .build())
//
//        responseMessageList.add(ResponseMessageBuilder()
//                .code(HttpStatus.FORBIDDEN.value())
//                .message(HttpStatus.FORBIDDEN.reasonPhrase)
//                .responseModel(ModelRef("ResponseWrap"))
//                .build())
//
//        responseMessageList.add(ResponseMessageBuilder()
//                .code(HttpStatus.NOT_FOUND.value())
//                .message(HttpStatus.NOT_FOUND.reasonPhrase)
//                .responseModel(ModelRef("ResponseWrap"))
//                .build())
//
//        responseMessageList.add(ResponseMessageBuilder()
//                .code(HttpStatus.INTERNAL_SERVER_ERROR.value())
//                .message(HttpStatus.INTERNAL_SERVER_ERROR.reasonPhrase)
//                .responseModel(ModelRef("ResponseWrap"))
//                .build())

        return Docket(DocumentationType.SWAGGER_2)  //Docket，Springfox的私有API设置初始化为Swagger2
//                .globalResponseMessage(RequestMethod.GET, responseMessageList)
//                .globalResponseMessage(RequestMethod.POST, responseMessageList)
//                .globalResponseMessage(RequestMethod.PUT, responseMessageList)
//                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.acheng1314.springboot2.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation::class.java))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .apiInfo(createApiInfo())
                .groupName("默认接口")
    }

    private fun createApiInfo() = ApiInfoBuilder()   //设置API文档的主体说明
            .title("acheng的SpringBoot探索之路ApiDocs")
            .description("acheng的SpringBoot探索之路")
            .version("v1.01")
            .termsOfServiceUrl("http://acheng1314.cn/")
            .build()

}


fun main(args: Array<String>) {
    runApplication<Springboot2Application>(*args)
}
